package com.hdu.other.tanxin;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class lc1353 {
    class Solution {
        public int maxEvents(int[][] events) {
           Arrays.sort(events, Comparator.comparingInt(a -> a[0]));
           int min = events[0][0];
           int max = events[0][1];
           for (int i = 1; i < events.length; i++) {
               max = Math.max(max, events[i][1]);
           }
           PriorityQueue<Integer> pq = new PriorityQueue<>();
           int cur = 0;
           int ans = 0;
           for (int day = min; day <= max; day++)
           {
               while (cur < events.length && events[cur][0] == day) {
                   pq.offer(events[cur++][1]);
               }
               while (!pq.isEmpty() && pq.peek() < day)
               {
                   pq.poll();
               }
               if (!pq.isEmpty()) {
                   pq.poll();
                   ans++;
               }
           }
           return ans;
        }
    }
}
